package com.ops.service;

import com.ops.entity.Hospital;
import com.ops.repository.HospitalRepository;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class HospitalService {
    
    @Autowired
    private HospitalRepository hospitalRepository;
    
    /**
     * 获取所有医院信息列表
     * @return 医院信息列表
     */
    @Transactional(readOnly = true)
    public List<Hospital> getAllHospitals() {
        return hospitalRepository.findAll();
    }

    /**
     * 从Excel文件导入医院信息
     * @param file Excel文件，包含医院名称和ID信息
     * @throws IOException 文件读取异常
     */
    @Transactional()
    public void importHospitals(MultipartFile file) throws IOException {
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);

        List<Hospital> hospitals = new ArrayList<>();

        for (Row row : sheet) {
            // 跳过表头
            if (row.getRowNum() == 0) continue;

            Cell nameCell = row.getCell(0);
            Cell idCell = row.getCell(1);

            if (nameCell == null || idCell == null) continue;

            String hospitalName = getCellValueAsString(nameCell);
            String hospitalId = getCellValueAsString(idCell);

            if (hospitalName.isEmpty() || hospitalId.isEmpty()) continue;

            // 检查医院ID是否已存在
            if (!hospitalRepository.existsByHospitalId(hospitalId)) {
                Hospital hospital = new Hospital();
                hospital.setHospitalName(hospitalName);
                hospital.setHospitalId(hospitalId);
                hospitals.add(hospital);
            }
        }

        if (!hospitals.isEmpty()) {
            hospitalRepository.saveAll(hospitals);
        }

        workbook.close();
    }

    /**
     * 将Excel单元格的值转换为字符串
     * @param cell Excel单元格
     * @return 单元格值的字符串表示
     */
    private String getCellValueAsString(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue().trim();
            case NUMERIC:
                return String.valueOf((long) cell.getNumericCellValue());
            default:
                return "";
        }
    }
        /**
     * 保存医院信息
     * @param hospital 待保存的医院信息
     * @return 保存后的医院信息
     */
    @Transactional
    public Hospital saveHospital(Hospital hospital) {
        return hospitalRepository.save(hospital);
    }

    /**
     * 检查医院ID是否已存在
     * @param hospitalId 医院ID
     * @return 如果医院ID已存在返回true，否则返回false
     */
    public boolean existsByHospitalId(String hospitalId) {
        return hospitalRepository.existsByHospitalId(hospitalId);
    }

    /**
     * 保存或更新医院信息
     * @param hospital 待保存或更新的医院信息
     * @return 保存或更新后的医院信息
     */
    public Hospital save(Hospital hospital) {
        return hospitalRepository.save(hospital);
    }

    /**
     * 根据医院ID删除医院信息
     * @param hospitalId 待删除的医院ID
     */
    @Transactional
    public void deleteByHospitalId(String hospitalId) {
        hospitalRepository.deleteByHospitalIdQuery(hospitalId);
    }
}